package com.yunzhou.action;

import com.yunzhou.common.DataChangeService;
import com.yunzhou.common.DataChangeServiceImpl;
import com.yunzhou.u8.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

/**
 * 销售合同评审流程
 * Date 2022-05-17
 * Created by dengxl
 */

public class createSalesContractProcessAction implements Action {

    private Log log = LogFactory.getLog(createSalesContractProcessAction.class.getName());

    public String execute(RequestInfo requestInfo) {

        String requestID = requestInfo.getRequestid();//请求id
        String workflowid = requestInfo.getWorkflowid();//workflowid
        String requestname = requestInfo.getRequestManager().getRequestname();// 请求标题

        log.info("createSalesContractProcessAction开始====" + requestID + "，workflowid：" + workflowid);

        String ywbk = "";//业务板块
        String xslx = "";//产品类型
        String htbh = "";//合同编号
        String xsry = "";//销售人员
        String xsbm = "";//销售人员部门
        String bz = "";//币种
        String hl = "";//汇率
        String fuktj = "";//收款条件
        String ystj = "";//验收条件
        String gdrq = "";//归档日期
        String liucbh = "";//流程编号
        String kh = "";//客户编码
        String kehmc = "";//客户名称
        String hedw = "";//合作单位
        String shenqr = "";//申请人

        //获取主表信息
        MainTableInfo mainTableInfo = requestInfo.getMainTableInfo();
        Property[] fields = mainTableInfo.getProperty();

        for (Property property : fields) {
            String name = property.getName();
            String value = property.getValue();
            if (name.equals(ywbk)) {
                ywbk = value;
            }
            if (name.equals("xslx")) {
                xslx = value;
            }
            if (name.equals("htbh")) {
                htbh = value;
            }
            if (name.equals("xsry")) {
                xsry = value;
            }
            if (name.equals("xsbm")) {
                xsbm = value;
            }
            if (name.equals("bz")) {
                bz = value;
            }
            if (name.equals("hl")) {
                hl = value;
            }
            if (name.equals("fuktj")) {
                fuktj = value;
            }
            if (name.equals("ystj")) {
                ystj = value;
            }
            if (name.equals("gdrq")) {
                gdrq = value;
            }
            if (name.equals("liucbh")) {
                liucbh = value;
            }
            if (name.equals("kh")) {
                kh = value;
            }
            if (name.equals("kehmc")) {
                kehmc = value;
            }
            if (name.equals("hedw")) {
                hedw = value;
            }
            if (name.equals("shenqr")) {
                shenqr = value;
            }
        }

        log.info("主表信息 - ywbk:" + ywbk + ",xslx:" + xslx + ",htbh:" + htbh + ",xsry:" + xsry + ",xsbm:" + xsbm + ",bz:" + bz
                + ",hl:" + hl + ",fuktj:" + fuktj + ",ystj:" + ystj + ",gdrq:" + gdrq + ",liucbh:" + liucbh + ",kh:" + kh + ",kehmc:" + kehmc + ",hedw:" + hedw + ",shenqr:" + shenqr);

        //数据转换
        DataChangeService dataChangeService = new DataChangeServiceImpl();
        String ywybm = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", xsry);//业务员编码(人员转换，取workcode)
        String zdrbm = dataChangeService.getSingleBrowseValue("workcode", "hrmresource", "id", shenqr);//制单人编码(人员转换，取workcode)
        String bz_1 = dataChangeService.getSingleBrowseValue("currencydesc", "FnaCurrency", "id", bz);//币种
        String ywgs = dataChangeService.getSingleBrowseValue("subcompanycode", "hrmsubcompany", "id", hedw);//业务公司
        String ywbk_1 = dataChangeService.getSelectNameById("15113", ywbk);//业务板块

        log.info("传入U8主表信息 - requestID:" + requestID + ",所属公司:" + ywgs  + ",合同编码:" + htbh + ",流程编号:" + liucbh + ",日期:" + gdrq
                + ",销售类型编码:" + (xslx.equals("1") ? "01" : "02") + ",业务员编码:" + (ywybm.isEmpty() ? "" : ywybm) + ",客户编码:" + kh
                + ",币种:" + (bz_1.isEmpty() ? "" : bz_1) + ",业务板块:" + ywbk_1 + ",收款条件:" + fuktj+ ",客户名称:" + kehmc + fuktj+ ",制单人:" + zdrbm);

        //对应U8主表
        SoVouchHead soVouch = new SoVouchHead();
        soVouch.setRequestid(requestID);//OA对应唯一ID
        soVouch.setShenqr(zdrbm.isEmpty() ? "" : zdrbm);//制单人
        soVouch.setDdate(gdrq);//日期
        soVouch.setHtcode(htbh);//合同编码
        soVouch.setLiucbh(liucbh);//流程编号
        soVouch.setCcuscode(kh);//客户编码
        soVouch.setCbustype("分期收款");//业务类型：分期收款
        soVouch.setCpersoncode(ywybm.isEmpty() ? "" : ywybm);//业务员编码
        soVouch.setCstcode(xslx.equals("1") ? "01" : "02");//销售类型编码
        soVouch.setCexch_name(bz_1.isEmpty() ? "" : bz_1);//币种
        soVouch.setYwbk(ywbk_1);//业务板块
        soVouch.setFkfs(fuktj);//付款方式
        soVouch.setYstj(ystj);//验收条件
        soVouch.setZdkh(kehmc);//客户名称

        //赋值明细（只赋一行）
        SoVouchBody[] soVouchBodies = new SoVouchBody[1];
        soVouchBodies[0] = new SoVouchBody();
        soVouchBodies[0].setIquantity("1");//数量
        soVouchBodies[0].setIprice("0");//单价
        soVouchBodies[0].setIsum("0");//金额
        soVouchBodies[0].setCinvcode("999999");//存货编码

        //赋值主表明细
        SoVouch soApp = new SoVouch();
        soApp.setCCompany(ywgs.isEmpty() ? "" : ywgs);//分公司
        soApp.setSoVouch(soVouch);
        soApp.setSoVouchs(soVouchBodies);

        String result = "";
        try {
            ServiceForOA workflowServiceXmlLocator = new ServiceForOALocator();//U8接口
            result = workflowServiceXmlLocator.getserviceForOASoap12().addSo(soApp);
        } catch (Exception ex) {
            log.info("输出有值，返回了错误====" + result);
            requestInfo.getRequestManager().setMessagecontent(result);
            ex.printStackTrace();
        }

        log.info("result====" + result);
        log.info("createSalesContractProcessAction结束====" + requestID);

        if (result.isEmpty()) {
            return SUCCESS;
        } else {
            requestInfo.getRequestManager().setMessagecontent(result);
            return FAILURE_AND_CONTINUE;
        }

    }

}
